blog

Home / DeveloperSection / Blogs / Basic Concept of List in c#

Basic Concept of List in c#

Abhishek Srivasatava 2234 19-Sep-2016

A list is the collection of data of the same datatype. It is similar to Array but easy to handle.

The syntax for the declaration of list // List<int>   list1= new List<int>();

Different way to add members :

1 : By using Add keyword

          list1.Add(7);
          list2.Add(8);
          list3.Add(9);

2 : Adding data at the time of object creation :

List<int> list1 = new List<int      

 Different way to display data

1: By using for loop .

We can get the number of member present in the list by the keyword Count.

Syntax: 

List<int> list1 = new List<int>(new int[]{7, 8, 9 });
int n = list1.count;
for ( int i=0;i<n;i++) Console.WriteLine(list[i]);

2* By using foreach loop :

foreach (int i in list)
{
Console.WriteLine(j);
}

How to clear the List?

List can be clear by using clear keyworld.

syntax //  list1.clear();

As list is similar to array so we can directly copy array to list

int[] arr = new int[3]; 
//defined the member of array by using the for loop.
List<int> list = new List<int>(arr); // Copy to List.

If we want to know the position of any member in thelist then we need to use Indexof .

syntax //  int k= list.IndexOf(9);

it will work fine if list contain only single element for the search element i.e., list contain only one '9'.

If search element is not found in the list then it will return '-1'.

If the search element is found in the list, but more than one place, then it will return '0'.

Inserting element in the list :

If list contain element like 2,3,6,8.

And if we want to insert a new element in between 3 and 6 then Insert is used

//List<int> list1 = new List<int>(new int[]{ 2,3,6,8 });
List1.insert(2,5); //2 is the position of the element.

 now new list will be 2,3,5,6,8

How to Reverse the String?
We can Reverse the string by using string keyword.

//list1.Reverse();

Here is the small program which may help you to clear above concept

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            List<int> list1 = newList<int>();
            list1.Add(7);
            list1.Add(8);
            list1.Add(9);
            Console.WriteLine("List1 element displaying by using foreach loop");
            foreach (int j in list1)
            {
              Console.WriteLine(j);
            }
            List<int> list2 = newList<int>(newint[]{ 2, 3, 6,8 });
            Console.WriteLine("List2 element displaying by using foreach loop");
            foreach (int l in list2)
            {
              Console.WriteLine(l);
            }
            int n = list1.Count;
            Console.WriteLine("List1 element displaying by using for loop");
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(list1[i]);
            }
            list2.Insert(2,5);
            int a = list1.Count();
            Console.WriteLine("List element displaying after Inserting an element ");
            foreach (int z in list2)
            {
             Console.WriteLine(z);
            }
            list1.Reverse();
            Console.WriteLine("List element displaying after Reversing the list ");
            foreach (int m in list1)
            {
                Console.WriteLine(m);
            }
            list1.Clear();
            Console.ReadLine();
        }
    }
}


Output:

Basic Concept of List in c#

Updated 16-Mar-2018

Leave Comment

Comments

Liked By